public class EqualsTest3 {

    public static void main(String[] args) {
        // 新建2个相同内容的Person对象，
        // 再用equals比较它们是否相等
        Person p1 = new Person("eee", 100);
        Person p2 = new Person("eee", 100);
        System.out.printf("p1.equals(p2) : %s\n", p1.equals(p2));
        System.out.printf("p1==p2 : %s\n", p1==p2);
    }

    private static class Person{
        int age;
        String name;

        public Person() {
        }

        public Person(String name,int age) {
            this.age = age;
            this.name = name;
        }

        public String toString() {
            return "Person{age = " + age + ", name = " + name + "}";
        }

        @Override
        public boolean equals(Object obj){
            if(obj==null){
                return false;
            }

            //如果是相同的对象返回true，反之返回false
            if(this==obj){
                return true;
            }

            //判断返回的类型
            if(this.getClass()!=obj.getClass()){
                return false;
            }

            Person person = (Person) obj;
            return name.equals(person.name) && age==person.age;
        }
    }
}
